package cn.edu.jxust.controller.api.air;

import cn.edu.jxust.common.domain.entity.AirType;
import cn.edu.jxust.common.domain.dto.AirTypeVo;
import cn.edu.jxust.common.domain.web.api.air.IAirTypeController;
import cn.edu.jxust.common.response.Result;
import cn.edu.jxust.common.response.ResultUtils;
import cn.edu.jxust.common.response.enums.ResultStatus;
import cn.edu.jxust.core.domain.repository.AirtypeService;
import com.baomidou.mybatisplus.core.metadata.IPage;
import lombok.extern.slf4j.Slf4j;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.RestController;
import javax.annotation.Resource;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author QiuKangming
 * @since 2020-06-29
 * @description 监测气体类型相关接口
 * 权限说明: 监测气体只有超级管理员或局级管理员可以设置
 */
@Slf4j
@RestController
public class AirTypeController implements IAirTypeController {

    @Resource
    private AirtypeService airtypeService;

    @Override
    public Result addAirType(AirTypeVo airTypeVo, BindingResult result){

        if(result.hasErrors()){
            return ResultUtils.resultWithoutData(ResultStatus.PARAM_ERROR);
        }
        try{
            return airtypeService.addAirType(airTypeVo);
        }catch (Exception e){
            log.error("AirTypeController --> addAirType: {}", e.getMessage());
            return ResultUtils.resultWithoutData(ResultStatus.FAILED);
        }

    }

    @Override
    public Result delAirType(Integer id){

        try{
            return airtypeService.removeById(id)
                    ? ResultUtils.resultWithoutData(ResultStatus.SUCCESS, "删除监测气体成功") :
                    ResultUtils.resultWithoutData(ResultStatus.FAILED, "删除监测气体失败");
        }catch (Exception e){
            log.error("AirTypeController --> delAirType: {}", e.getMessage());
            return ResultUtils.resultWithoutData(ResultStatus.FAILED);
        }

    }

    @Override
    public Result modifyAirType(AirTypeVo airTypeVo, BindingResult result){

        if(airTypeVo.getId() == null || result.hasErrors()){
            return ResultUtils.resultWithoutData(ResultStatus.PARAM_ERROR);
        }
        try{
            return airtypeService.updateById(
                    AirType.builder()
                            .id(airTypeVo.getId())
                            .airName(airTypeVo.getAirName())
                            .editor(airTypeVo.getEditor())
                            .build())
                    ? ResultUtils.resultWithoutData(ResultStatus.SUCCESS, "修改监测气体成功")
                    : ResultUtils.resultWithoutData(ResultStatus.FAILED, "修改监测气体失败");
        }catch (Exception e){
            log.error("AirTypeController --> modifyAirType: {}", e.getMessage());
            return ResultUtils.resultWithoutData(ResultStatus.FAILED);
        }

    }

    @Override
    public Result findAirTypePage(String airName, Integer page, Integer size){

        try{
            IPage<AirType> airTypeIPage = airtypeService.listAirType(airName, page, size);
            if(airTypeIPage.getRecords() != null && !airTypeIPage.getRecords().isEmpty()){
                return ResultUtils.resultWithData(ResultStatus.SUCCESS, airTypeIPage);
            }else {
                return ResultUtils.resultWithoutData(ResultStatus.NOT_FOUND, "未找到任何监测气体信息");
            }
        }catch (Exception e){
            log.error("[AreaController --> findAreaList 异常]: {}", e.getMessage());
            return ResultUtils.resultWithoutData(ResultStatus.FAILED);
        }

    }

}
